<!DOCTYPE html>
<html>
<head>
    <title>经书下载器-漫画目录模式</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <script src="./js/jszip.min.js" ></script>
    <script src="./js/FileSaver.min.js" ></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            font-size: 14px;
        }

        .form-container {
            max-width: 450px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        input {
            font-size: 14px;
        }

        h2 {
            text-align: center;
        }

        a {
            margin-left: 40px;
        }

        label {
            display: block;
            margin-bottom: 10px;
        }

        input[type="text"],
        input[type="submit"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        input[type="submit"] {
            background-color: #4CAF50;
            color: #fff;
            cursor: pointer;
        }

        input[type="number"],
        input[type="text"],
        input[type="button"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        input[type="button"] {
            background-color: #4CAF50;
            color: #fff;
            cursor: pointer;
        }

        pre {
            font-size: 12px;
        }
    </style>
    <script src="./js/jquery.js"></script>
</head>
<body>
<div class="form-container">
    <div class="h2 text-center">
        <button class="btn btn-sm btn-default pull-left"
        nclick="uniPage()">菜单</button>经书下载器</div>
    <ul class="nav nav-tabs">
<!--        <li class="active">-->
<!--            <a id="aindex" href="index.html">目录模式</a>-->
<!--        </li>-->
        
    </ul>

    <form id="bookForm">
        <label for="name">漫画名:</label>
        <input type="text" id="name" name="name" placeholder="输入漫画名" required><br><br>
        <label for="url"><span class="text-danger">*</span>目录链接:</label>
        <input type="text" id="url" name="url" placeholder="" onblur="check_src(0)" required>
        <span class="help-block">链接中m.推荐换成 www.,打开时有所有章节名的才可用</span>
        <label for="tags">保留的html标签,分割:</label>
        <input type="text" id="tags" name="tags"
               value="h1,img@src" required>
        <span class="help-block"> 不清楚的话html标签使用默认值，不用修改</span>
        <label for="submenu">目录分页关键词:</label>
        <input type="text" id="submenu" name="submenu" placeholder="">
        <span class="help-block">目录有拆分多页时才需输入，页面显示下一页的文本如：下一页</span>
        <label for="subpage">章节分页关键词:</label>
        <input type="text" id="subpage" name="subpage" placeholder="">
        <span class="help-block">章节有拆分多页时才需输入，页面显示下一页的文本如：下一页</span>
        <label for="prefix">链接关键词:</label>
        <input type="text" id="prefix" name="prefix" placeholder="">
        <span class="help-block">无法解析目录时使用,尝试填入目录链接里最后书的数字id</span>
        <label for="proc">并发数:</label>
        <input type="number" id="proc" name="proc" placeholder="1-30" value=15 required>
        <span class="help-block">同时进行数量1-30</span>
        <label for="proc">图片链接替换:</label>
        <input type="text" id="raw" name="raw" placeholder="原字符串" value="" >
        <br><br>
        <input type="text" id="place" name="place" placeholder="替换后字符串" value="">
        <span class="help-block">链接原字符串变替换后字符串</span>
        <label for="sort">章节顺序:</label>
        <select name="sort" required>
            <option value="0">增加</option>
            <option value="1">减少</option>
        </select>
        <label for="down">下载模式:</label>
        <select name="down" required>
			<option value="server">服务器中转</option>
            <option value="">本地直连</option>   
        </select>
        <span class="help-block">网页端需要选服务器中转，或者本地直连谷歌快捷方式目标要加 --disable-web-security --user-data-dir=C:\jiuChromeUserData</span>
        <label for="start">章节按范围下载(开始-结束，留空下载全部章节):</label>
        <input type="number" style="width: 40%;" id="start" name="start" placeholder="开始序号,从0开始" value="" required>-
        <input type="number" style="width: 40%;" id="end" name="end" placeholder="结束序号,0不限制" value="" required>
        			
		<br><br>
        <input type="button" value="提交" onclick="submitForm()">

    </form>
    <div id="apiCbk"></div>
</div>
<div class="alert alert-warning">
    保存根目录或/cyberbook,需要文件权限
</div>
<h2 class="text-left"><a id="ahistory" href="comic_history.html">历史记录(<span id="history_num"></span>)</a> <a
        class="pull-right" style="margin-right: 5vw;" href="res.html">资源</a></h2>
<h2 class="text-left"><a href="praise.html">赞赏</a><a
        href="https://spider.sumroads.site/jsnes/index.html">NES</a></h2>

<script src="./js/hybrid_html_uni.webview.1.5.5.js"></script>
<script src="./js/common.js"></script>
<script src="./js/jquery.js"></script>
<script>
    var isSubmit = false;
    var his = getHistory("comic")
    var v = GetParam('v');
  //  document.getElementById("aindex").href += "?v=" + v
    //document.getElementById("alist").href += "?v=" + v
    document.getElementById("ahistory").href += "?v=" + v

    if (his) {
        document.getElementById("history_num").innerHTML = his.length
    } else {
        document.getElementById("history_num").innerHTML = 0
    }

    function submitForm() {
        if (isSubmit) {
            return;
        }

        var form = document.getElementById("bookForm");
        var name = form.elements["name"].value;
        var url = form.elements["url"].value;
        var sort = form.elements["sort"].value;
        var tags = form.elements["tags"].value;
        var subpage = form.elements["subpage"].value;
        var submenu = form.elements["submenu"].value;
        var prefix = form.elements["prefix"].value;
        var proc = form.elements["proc"].value;
        var raw = form.elements["raw"].value;
        var place = form.elements["place"].value;
        var down = form.elements["down"].value;
		var start = form.elements["start"].value;
		var end = form.elements["end"].value;
        if (!name) {
            alert("请输入任意漫画名")
            return
        }
        isSubmit = true;
        document.getElementById("apiCbk").innerHTML = "加载中:";
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "/api/comicMenu", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                // alert("Form submitted successfully!");
                var res = JSON.parse(xhr.responseText)
                if (res.Code == 0) {
                    console.log(xhr.responseText)
                    tpl = res.Msg;
                    if (res.Data) {
                        downPage(res.Data)
                    } else {

                    }
                    document.getElementById("apiCbk").innerHTML = tpl

                } else {
                    document.getElementById("apiCbk").innerHTML = res.Msg;
                }
                isSubmit = false;
            } else if (xhr.readyState === 4) {
                alert("Error: " + xhr.responseText);
                isSubmit = false;
            }
        };
        //JSON.stringify()
        var data = {
            "name": name,
            "url": url,
            "sort": sort,
            "tags": tags,
            "subpage": subpage,
            "submenu": submenu,
            "proc": proc,
            "prefix": prefix,
            "raw":raw,
            "place":place,
            'down':down,
			"start": start,
			"end": end
        };
        xhr.send(objectToFormUrlEncoded(data));
        var history = getHistory("comic")
        history.push(data)
        saveHistory(history,"comic")
        document.getElementById("history_num").innerHTML = history.length
    }

    function downPage(urls) {
        var form = document.getElementById("bookForm");
        var name = form.elements["name"].value;
        var url = form.elements["url"].value;
        var sort = form.elements["sort"].value;
        var tags = form.elements["tags"].value;
        var subpage = form.elements["subpage"].value;
        var submenu = form.elements["submenu"].value;
        var prefix = form.elements["prefix"].value;
        var proc = form.elements["proc"].value;
        var raw = form.elements["raw"].value;
        var place = form.elements["place"].value;
        var down = form.elements["down"].value;
        //document.getElementById("apiCbk").innerHTML = "请勿离开页面,转存中";
        $("#apiCbk").html( "请勿离开,解析中");
        var fin = 0;
        var len = urls.length;
        var imgurls= [];
        var text="";
        urls.forEach(function (url,kk) {

            var xhr = new XMLHttpRequest();
            xhr.open("POST", "/api/comicPage",false);

            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    fin+=1;
                   // document.getElementById("apiCbk").innerHTML = "请勿离开,解析章节中("+fin+"/"+len+")";
                    $("#apiCbk").html( "请勿离开,解析章节中("+fin+"/"+len+")");
                    var res = JSON.parse(xhr.responseText)
                    if(down!="server"&&res.Code == 0){
                        text+=JSON.stringify(res.Data)+'\r\n';
                        for (var iv of res.Data.Content){
                            if (iv==""){
                                continue;
                            }
                            imgurls.push(iv)
                        }
                    }

                    if (fin>=len) {
                        if (down == 'server'){
                             comicdown(name,v)
                        }else{

                            downloadZip(name, imgurls,text,"#apiCbk")
                        }
                    }
                } else if (xhr.readyState === 4) {
                    alert("Error: " + xhr.responseText);
                    isSubmit = false;
                }
            }
            var data = {
                "name": name,
                "url": url,
                "sort": sort,
                "tags": tags,
                "subpage": subpage,
                "submenu": submenu,
                "proc": proc,
                "prefix": prefix,
                "raw": raw,
                "place": place,
                'key':kk,
                'down':down
            };
            xhr.send(objectToFormUrlEncoded(data));
        })


    }
   // comicdown("归还")

    

</script>
</body>
</html>